﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json;
using Model;
using BLL;

namespace Inventory.view
{
    public partial class MyGoods : System.Web.UI.Page
    {
        public int sellerId;
        protected void Page_Load(object sender, EventArgs e)
        {
            Seller model = (Seller)Session["CurrentUser"];
            if (model == null)
            {
                //Response.Write("<script>alert(\"请登录系统\");window.location.href=\"login.aspx\"</script>");
                //Response.End();
            }
            //sellerId = model.Id;
            if (Request["action"] != null && Request["action"] == "getClass")
            {
                //获取分类
                GetClass();
            }
            else if (Request["action"] != null && Request["action"] == "getList")
            {
                GetInfoList();
            }
            else if (Request["action"] != null && Request["action"] == "getDtl")
            {
                //获得商品明细信息
                GetDtl();
            }
            else if (Request["action"] != null && Request["action"] == "showinfo")
            {
                //弹窗消息
                ShowInfo();
            }
        }

        //弹窗消息
        private void ShowInfo()
        {
            MySubscribeBll bll = new MySubscribeBll();
            MySubscribe m = new MySubscribe();
            m.Id = Convert.ToInt32(Request["id"]);
            m.IsNeedShow = Request["isneedshow"].ToString() == "1" ? true : false;
            m.ShowInNum = Convert.ToInt32(Request["lessnum"]);
            bll.AddToShow(m);
            Response.Write("操作成功！");
            Response.End();
        }

        private void GetInfoList()
        {
            MySubscribeBll bll = new MySubscribeBll();
            MySubscribe m = new MySubscribe();
            if (!string.IsNullOrEmpty(Request["aliww"]))
            {
                m.SellerInfo.Aliww = Request["aliww"];
            }
            if (!string.IsNullOrEmpty(Request["classId"]))
            {
                m.GoodsInfo.ClassId = Request["classId"].ToString() == "" ? 0 : Convert.ToInt32(Request["classId"].ToString());
            }
            if (!string.IsNullOrEmpty(Request["goodsName"]))
            {
                m.GoodsInfo.GoodsName = Request["goodsName"];
            }
            if (!string.IsNullOrEmpty(Request["no"]))
            {
                m.GoodsInfo.No = Request["no"];
            }
            m.SellerId = sellerId;//当前登录人
            List<MySubscribe> list = new List<MySubscribe>();
            list = bll.ListMyGoods(m);
            IsoDateTimeConverter timeConverter = new IsoDateTimeConverter();//对时间进行格式化
            timeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
            string jsonText = JsonConvert.SerializeObject(list, Formatting.Indented, timeConverter);
            Response.Write(jsonText);
            Response.End();
        }

        private void GetClass()
        {
            GoodsClassfyBll bllClass = new GoodsClassfyBll();
            List<MGoodsClassfy> list = new List<MGoodsClassfy>();
            MGoodsClassfy mclass = new MGoodsClassfy();
            mclass.SellerId = 0;//这里到时候要改成读取当前登录的店铺信息
            list = bllClass.ListClassByGroup(mclass);
            string jsonText = JsonConvert.SerializeObject(list);
            Response.Write(jsonText);
            Response.End();
        }

        //获得明细
        private void GetDtl()
        {
            GoodsInfoBll bll = new GoodsInfoBll();
            GoodsDtlInfo m = new GoodsDtlInfo();
            List<GoodsDtlInfo> list = new List<GoodsDtlInfo>();
            list = bll.ListGoodsDtlInfo(Request["goodsId"]);
            string jsonText = JsonConvert.SerializeObject(list);
            Response.Write(jsonText);
            Response.End();
        }
    }
}